package com.itboye.lottery.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.itboye.lottery.R;
import com.itboye.lottery.activity.TouZhuListActivity;
import com.itboye.lottery.bean.CodeBean;
import com.itboye.lottery.view.FlowLayout;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by wu on 2018/1/9.
 */

public class TouZhuListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final ArrayList<HashMap<String, Object>> codeBeanArrayList;
    private Activity activity;
    private Context context;
    private boolean lotteryType;

    public TouZhuListAdapter(Activity activity, ArrayList<HashMap<String, Object>> codeBeanArrayList) {
        this.activity = activity;
        this.codeBeanArrayList = codeBeanArrayList;
    }

    public void setLotteryType(boolean lotteryType) {
        //type:true 双色球  false：大乐透
        this.lotteryType = lotteryType;
    }

    public OnItemClickListener itemClickListener;

    public void setOnItemClickListener(TouZhuListAdapter.OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void clearAllStatus() {
//        for (int i = 0; i < codeBeanArrayList.size(); i++) {
//            if (codeBeanArrayList.get(i).isSelect()) {
//                codeBeanArrayList.get(i).setSelect(false);
//            }
//        }
        notifyDataSetChanged();
    }

    public void randomCodeUpdate(ArrayList<CodeBean> randomCodeBeanArrayList) {
        clearAllStatus();//先清除原来的所有状态
//        for (int i = 0; i < randomCodeBeanArrayList.size(); i++) {
//            int randomCodeValue = randomCodeBeanArrayList.get(i).getCodeValue();
//            for (int i1 = 0; i1 < codeBeanArrayList.size(); i1++) {
//                int codeValue = codeBeanArrayList.get(i1).getCodeValue();
//                if (randomCodeValue == codeValue) {
//                    codeBeanArrayList.get(i1 + 1).setSelect(true);
//                }
//            }
//
//        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View v = LayoutInflater.from(context).inflate(R.layout.item_touzhu_list, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        Object codeMap = codeBeanArrayList.get(position);
        ArrayList<CodeBean> redCodeArray = ((HashMap<String, ArrayList<CodeBean>>) codeMap).get("red");//红色号码
        ArrayList<CodeBean> redTuoCodeArray = ((HashMap<String, ArrayList<CodeBean>>) codeMap).get("red_tuo");//红拖
        ArrayList<CodeBean> blueCodeArray = ((HashMap<String, ArrayList<CodeBean>>) codeMap).get("blue");//蓝
        ArrayList<CodeBean> blueTuoCodeArray = ((HashMap<String, ArrayList<CodeBean>>) codeMap).get("blue_tuo");//蓝
        int zhushu = 1;
        try {
            zhushu = ((HashMap<String, Integer>) codeMap).get("zhushu");
        } catch (Exception e) {

        }
        createCodeData(viewHolder, viewHolder.fl_blue_ball, blueCodeArray, 2);//蓝色球
        createCodeData(viewHolder, viewHolder.fl_red_ball, redCodeArray, 0);//红色球
        if (redTuoCodeArray == null) {
            viewHolder.tr_hong_tuo.setVisibility(View.GONE);
            viewHolder.tv_bet_type.setText("普通投注  " + "" + zhushu + "注  " + zhushu * 2 + "元");//显示投注类型，如单式 复式
        } else {
            createCodeData(viewHolder, viewHolder.fl_red_tuo_ball, redTuoCodeArray, 1);//托马
            viewHolder.tr_hong_tuo.setVisibility(View.VISIBLE);
        }
        if (blueTuoCodeArray == null) {//表示为大乐透
            //没有说明是普通投注 分为普通投注 红球复试 蓝球复式
            viewHolder.tr_blue_tuo.setVisibility(View.GONE);
        } else {
            //一旦有蓝色拖码说明是全复式
            viewHolder.tv_touzhu_list_red_ball.setText("红(胆)");
            viewHolder.tv_touzhu_list_red_tuo_ball.setText("红(拖)");
            viewHolder.tv_touzhu_list_blue_ball.setText("蓝(胆)");
            createCodeData(viewHolder, viewHolder.fl_blue_tuo_ball, blueTuoCodeArray, 2);//托马
            viewHolder.tv_bet_type.setText("胆拖  " + "" + zhushu + "注  " + zhushu * 2 + "元");//显示投注类型，如单式 复式
            viewHolder.tr_hong_tuo.setVisibility(View.VISIBLE);
        }
        viewHolder.iv_touzhu_list_delete.setTag(position);


        if (lotteryType) {
            if (redTuoCodeArray == null) {
                if (redCodeArray.size() <= 6 && blueCodeArray.size() <= 1) {
                    viewHolder.tv_bet_type.setText("单式  " + "" + zhushu + "注  " + zhushu * 2 + "元");
                } else if (redCodeArray.size() > 6 && blueCodeArray.size() > 1) {
                    viewHolder.tv_bet_type.setText("全复式  " + "" + zhushu + "注  " + zhushu * 2 + "元");
                } else if (blueCodeArray.size() > 1) {
                    viewHolder.tv_bet_type.setText("蓝球复式  " + "" + zhushu + "注  " + zhushu * 2 + "元");
                } else if (redCodeArray.size() > 6) {
                    viewHolder.tv_bet_type.setText("红球复式  " + "" + zhushu + "注  " + zhushu * 2 + "元");
                }
            } else {
                viewHolder.tv_bet_type.setText("胆拖  " + "" + zhushu + "注  " + zhushu * 2 + "元");//显示投注类型，如单式 复式
            }
        } else {
            if (blueTuoCodeArray == null) {
                if (redCodeArray.size() <= 5 && blueCodeArray.size() <= 2) {
                    viewHolder.tv_bet_type.setText("单式  " + "" + zhushu + "注  " + zhushu * 2 + "元");
                } else if (redCodeArray.size() > 5&&blueCodeArray.size() > 2) {
                    viewHolder.tv_bet_type.setText("全复式  " + "" + zhushu + "注  " + zhushu * 2 + "元");
                } else if (blueCodeArray.size() > 2) {
                    viewHolder.tv_bet_type.setText("蓝球复式  " + "" + zhushu + "注  " + zhushu * 2 + "元");
                } else if(redCodeArray.size() > 5){
                    viewHolder.tv_bet_type.setText("红球式  " + "" + zhushu + "注  " + zhushu * 2 + "元");
                }
            }
        }
    }

    private String judgeBetType() {
        return "";
    }

    public int caculateTotalBet() {
        int zhushu = 0;
        for (HashMap<String, Object> stringObjectHashMap : codeBeanArrayList) {
            zhushu += (int) (stringObjectHashMap).get("zhushu");
        }
        return zhushu;
    }

    void createCodeData(ViewHolder viewHolder, FlowLayout flowLayout, ArrayList<CodeBean> codeBeanArrayList, int type) {
//        switch (type) {
//            case 0:
//                break;
//            case 1:
//                break;
//            case 2:
//                viewHolder.tr_hong_tuo.setVisibility(View.VISIBLE);
//                break;
//        }
        flowLayout.removeAllViews();
        for (int i = 0; i < codeBeanArrayList.size(); i++) {
            TextView textView = new TextView(activity);
            if (type == 0 || type == 1) {
                //红球
                textView.setBackgroundResource(R.mipmap.hongqiuxuanzhong);
                textView.setTextColor(activity.getResources().getColor(R.color.white));
            } else if (type == 2) {
                //蓝球
                textView.setBackgroundResource(R.mipmap.lanqiuxuanzhong);
                textView.setTextColor(activity.getResources().getColor(R.color.white));
                viewHolder.tr_hong_tuo.setVisibility(View.VISIBLE);
            } else {
                //红托
            }
            textView.setText(codeBeanArrayList.get(i).getCodeValue() + "");
            textView.setGravity(Gravity.CENTER);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.setMargins(5, 5, 5, 5);
            flowLayout.addView(textView, layoutParams);
        }
    }

    @Override
    public int getItemCount() {
        return codeBeanArrayList == null ? 0 : codeBeanArrayList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public ImageView iv_touzhu_list_delete;
        public TextView tv_touzhu_list_red_ball, tv_touzhu_list_red_tuo_ball;//红胆红托
        public TextView tv_touzhu_list_blue_ball, tv_touzhu_list_blue_tuo_ball;//蓝胆蓝托
        TextView tv_bet_type;//投注类型
        FlowLayout fl_red_ball;//红色
        FlowLayout fl_red_tuo_ball; //红拖
        FlowLayout fl_blue_ball;//蓝色
        FlowLayout fl_blue_tuo_ball;//蓝托

        TableRow tr_hong_tuo, tr_blue_tuo;//红托 蓝托

        public ViewHolder(View itemView) {
            super(itemView);
            iv_touzhu_list_delete = itemView.findViewById(R.id.iv_touzhu_list_delete);
            tv_touzhu_list_red_ball = itemView.findViewById(R.id.tv_touzhu_list_red_ball);
            tv_touzhu_list_red_tuo_ball = itemView.findViewById(R.id.tv_touzhu_list_red_tuo_ball);

            tv_touzhu_list_blue_ball = itemView.findViewById(R.id.tv_touzhu_list_blue_ball);
            tv_touzhu_list_blue_tuo_ball = itemView.findViewById(R.id.tv_touzhu_list_blue_tuo_ball);

            fl_red_ball = itemView.findViewById(R.id.fl_red_ball);
            fl_red_tuo_ball = itemView.findViewById(R.id.fl_red_tuo_ball);
            fl_blue_ball = itemView.findViewById(R.id.fl_blue_ball);
            fl_blue_tuo_ball = itemView.findViewById(R.id.fl_blue_tuo_ball);
            tr_hong_tuo = itemView.findViewById(R.id.tr_hong_tuo);
            tr_blue_tuo = itemView.findViewById(R.id.tr_blue_tuo);

            tv_bet_type = itemView.findViewById(R.id.tv_bet_type);
            iv_touzhu_list_delete.setOnClickListener((TouZhuListActivity) activity);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (itemClickListener != null) {
                itemClickListener.onItemClick(v, getAdapterPosition());
            }
        }
    }
}
